package com.zte.smbms_plus.controller;


import com.sun.org.apache.xpath.internal.operations.Mod;
import com.zte.smbms_plus.pojo.SmbmsAddress;
import com.zte.smbms_plus.service.SmbmsAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/address/")
public class Addresscontroller {
    @Autowired SmbmsAddressService addressService;

    @RequestMapping("list")
    public String list(Model model){
        List<SmbmsAddress> addlist=addressService.list();
        model.addAttribute("address",addlist);
        return "addlist";
    }

    @RequestMapping("details/{id}")
    public String List(@PathVariable("id")Long uid,Model model){
        /*List<SmbmsAddress> addlist=addressService.findAddressOrById(uid);*/
        SmbmsAddress byId = addressService.getById(uid);
        model.addAttribute("address",byId);
        return "addview";
    }


    @RequestMapping("updateStep/{id}")
    public String updateList(@PathVariable("id") Long aid,Model model){
        /*List<SmbmsAddress> addlist=addressService.findAddressOrById(aid);*/
        SmbmsAddress byId = addressService.getById(aid);
        model.addAttribute("address",byId);
        return "addressmodify";
    }

    @RequestMapping("updateSave")
    public String updateSave(SmbmsAddress address,Model model){
        boolean flag= addressService.updateAddress(address);
        if (flag){
            return "forward:list";
        }else {
            model.addAttribute("修改失败");
            return "error";
        }
    }


    @RequestMapping("delete")
    public String delete(Long id, Model model){
        boolean flag=addressService.delAddress(id);
        if (flag){
            model.addAttribute("删除成功");
            return "redirect:list";
        }else {
            model.addAttribute("删除失败");
            return "error";
        }
    }

    @RequestMapping("Addaddress")
    public String Addaddress(SmbmsAddress smbmsAddress,Model model){
        boolean flag= addressService.addAddress(smbmsAddress);
        if (flag){
            model.addAttribute("添加成功");
            return "forward:list";
        }else{
            model.addAttribute("添加失败");
            return "error";
        }
    }
}
